use actix_web::{web, HttpResponse};

use crate::{services::email_service::EmailService, AppResult, ArcConfig};

use super::OK_RESPONSE;

mod ctrl_struct {
    define_request_struct!(SendRegisterCodeReq { email: String });
}

async fn send_register_code(
    svr: web::Data<EmailService>,
    req: web::Json<ctrl_struct::SendRegisterCodeReq>,
) -> AppResult<HttpResponse> {
    svr.send_register_code(req.into_inner().email).await?;
    Ok(HttpResponse::Ok().json(OK_RESPONSE))
}
async fn valid_is_enabled(config: web::Data<ArcConfig>) -> HttpResponse {
    HttpResponse::Ok().json(serde_json::json!({
        "enabled":config.app.enable_email_validation
    }))
}

pub fn configure_email_controller(cfg: &mut web::ServiceConfig) {
    cfg.route("/valid", web::post().to(send_register_code))
        .route("/valid/enabled", web::post().to(valid_is_enabled));
}
